﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class Comment_AddController : UserController
    {
        public IArticleService _article { get; set; }
        public IArticle_commentService _article_comment { get; set; }
        [HttpGet]
        public IActionResult Get(int article_id,string content)
        {
            Article_comment model = new Article_comment();
            if (article_id == 0)
            {
                return Ok(new { status=0,msg= "信息参数传输有误！" });
            }
            if (string.IsNullOrEmpty(content))
            {
                return Ok(new { status = 0, msg = "请输入评论的内容！" });
            }
            //检查该文章是否存在
            Article artModel = _article.GetModel(article_id);
            if (artModel == null)
            {
                return Ok(new { status = 0, msg = "主题不存在或已删除！" });
            }
            //检查用户是否登录
            int user_id = 0;
            string user_name = "匿名用户";
            Users userModel = GetUserInfo();
            if (userModel != null)
            {
                user_id = userModel.id;
                user_name = userModel.user_name;
            }
            var sys = Common.XMLConfig.Sysconfig.loadConfig(_env.ContentRootPath);
            model.channel_id = artModel.channel_id;
            model.article_id = artModel.id;
            model.content = content;
            model.user_id = user_id;
            model.user_name = user_name;
            model.user_ip = CMSRequest.GetClientUserIp(HttpContext);
            model.is_lock = sys.commentstatus; //审核开关
            model.add_time = DateTime.Now;
            model.is_reply = 0;
            if (_article_comment.Add(model) > 0)
            {
                return Ok(new { status = 1, msg = "评论提交成功！" });
            }
            return Ok(new { status = 0, msg = "保存过程中发生错误！" });
        }
    }
}